// 查询

// swagger 文档
defineRouteMeta({
  openAPI: {
    description: '用户id查询',
    summary: '用户id查询',
    tags: ['用户'],
    parameters: [
      {
        name: 'id',
        in: 'path',
        description: '用户id',
        required: true,
        schema: {
          type: 'number'
        }
      }
    ],
    responses: {
      200: {
        description: '用户查询成功',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 200
                },
                message: {
                  type: 'string',
                  description: '状态信息'
                },
                data: {
                  type: 'object',
                  description: '用户信息',
                  properties: {
                    id: {
                      type: 'number',
                      description: '用户id'
                    },
                    name: {
                      type: 'string',
                      description: '用户名称'
                    },
                    email: {
                      type: 'string',
                      description: '用户邮箱'
                    },
                    password: {
                      type: 'string',
                      description: '用户密码'
                    },
                    avatar: {
                      type: 'string',
                      description: '用户头像'
                    },
                    createdAt: {
                      type: 'string',
                      description: '用户创建时间'
                    }
                  }
                }
              }
            }
          }
        }
      },
      404: {
        description: '用户不存在',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 404
                },
                message: {
                  type: 'string',
                  description: '状态信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      },
      500: {
        description: '用户查询失败',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 500
                },
                message: {
                  type: 'string',
                  description: '错误信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      }
    }
  }
});

import { and, eq } from 'drizzle-orm';
export default eventHandler(async event => {
  const { result } = new useResult(event);
  const { id } = getRouterParams(event);
  try {
    const users = await useDrizzle()
      .select()
      .from(tables.users)
      .where(and(eq(tables.users.id, Number(id))));
    if (!users.length) {
      return result(null, '用户不存在', 404);
    }
    return result(users[0]);
  } catch (e: any) {
    return result(null, e.message, 500);
  }
});
